package com.key.service.impl;

import com.key.common.R;
import com.key.common.TranException;
import com.key.common.vo.AssetModelVO;
import com.key.entity.AssetModel;
import com.key.mapper.AssetModelMapper;
import com.key.service.AssetModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AssetModelServiceImpl implements AssetModelService {
    @Autowired
    private AssetModelMapper assetModelMapper;
    @Override
    public R addAssetModel(AssetModel assetModel) {
        R r=new R();
        AssetModel assetModel1 = assetModelMapper.selectAssetModelByAssetModelName(assetModel.getAssetName());
        if (assetModel1==null){
            int i = assetModelMapper.insertAssertModel(assetModel);
            return i>0?R.ok():R.no();
        }else {
            r.setCode(200);
            r.setMsg("已存在");
        }
        return r;
    }

    @Override
    public R findAllAssetModelByAssetTypeId(Integer assetTypeId) {
        List<AssetModel> assetModels = assetModelMapper.selectAllAssetModelByAssetTypeId(assetTypeId);
        R r=R.ok();
        r.setData(assetModels);
        return r;
    }

    @Override
    public R findAllAssetModelById(Integer id) {
        AssetModel model = assetModelMapper.findAllAssetModelById(id);
        R r=R.ok();
        r.setData(model);
        return r;
    }

    @Override
    public R updateAssetModel(AssetModel assetModel) {
        int i = assetModelMapper.updateAssetModel(assetModel);
        return i>0?R.ok():R.no();
    }

    @Override
    public R disableAssetModel(Integer id) {
        int i = assetModelMapper.disableAssetModel(id);
        return i>0?R.ok():R.no();
    }

    @Override
    public R enableAssetModel(Integer id) {
        int i = assetModelMapper.enableAssetModel(id);
        return i>0?R.ok():R.no();
    }

    @Override
    @Transactional
    public R deleteAssetModel(List<Integer> ids) {
        R r=new R();
        int i=0;
        for (Integer id : ids) {
            int j = assetModelMapper.deleteAssetModelById(id);
            i+=j;
        }
        if (i==ids.size()){
            r=R.ok();
        }else {
            throw new TranException("删除异常");
        }
        return r;
    }

    @Override
    public R selectAssetModel(AssetModelVO assetModelVO) {
        R r=new R();
        List<AssetModel> assetModels = assetModelMapper.selectAssetModel(assetModelVO);
        if (assetModels.size()>0) {
            r=R.ok();
            r.setData(assetModels);
        }else {
            r.setCode(200);
            r.setMsg("无符合数据");
        }
        return r;
    }
}
